/*
 * Copyright © 2018 iFund. All rights reserved.
 */

package com.climber.android.commonres.ui

import android.app.AlertDialog
import android.content.Intent
import android.util.TypedValue
import android.view.View
import android.widget.Button
import android.widget.TextView
import cat.ereza.customactivityoncrash.CustomActivityOnCrash
import com.climber.android.commonres.R
import com.climber.android.commonsdk.app.AppConstants
import com.climber.android.commonsdk.app.AppEnv
import com.umeng.analytics.MobclickAgent
import io.ganguo.library.klog.KLog
import io.ganguo.library.mvp.ui.mvp.IPresenter
import io.ganguo.library.mvp.ui.mvp.di.component.AppComponent
import io.ganguo.library.mvp.util.StatusBarUtils
import io.ganguo.library.mvp.util.ToastHelper

/**
 * Created by mikes on 2017-8-21.
 */
class ErrorActivity : BaseMVPActivity<IPresenter>() {
    override fun setupActivityComponent(appComponent: AppComponent) {
    }

    override fun getLayoutResourceId(): Int = R.layout.com_activity_error

    override fun initView() {
        StatusBarUtils.changeStatusBarColor(this, resources.getColor(R.color.status_bar_color))
    }

    override fun initListener() {
    }

    override fun initData() {
        //Close/restart button logic:
        //If a class if set, use restart.
        //Else, use close and just finish the app.
        //It is recommended that you follow this logic if implementing a custom error activity.
        val restartButton = findViewById<View>(R.id.error_activity_restart_button) as Button

        val restartActivityClass = CustomActivityOnCrash.getRestartActivityClassFromIntent(intent)

        if (restartActivityClass != null) {
            restartButton.setText(R.string.error_activity_restart_app)
            restartButton.setOnClickListener {
                val intent = Intent(this@ErrorActivity, restartActivityClass)
                finish()
                startActivity(intent)
            }
        } else {
            restartButton.setOnClickListener { finish() }
        }

        val errorMsg = CustomActivityOnCrash.getAllErrorDetailsFromIntent(this@ErrorActivity, intent)
        KLog.e(AppConstants.LOG_TAG,errorMsg)

        MobclickAgent.reportError(this, errorMsg)

        val moreInfoButton = findViewById<View>(R.id.error_activity_more_info_button) as Button

        if(AppEnv.DEV_MODE){
            moreInfoButton.visibility = View.VISIBLE
        } else{
            moreInfoButton.visibility = View.GONE
        }

        moreInfoButton.setOnClickListener {
            //We retrieve all the error data and show it

            val dialog = AlertDialog.Builder(this@ErrorActivity)
                .setTitle(R.string.error_activity_error_details_title)
                .setMessage(errorMsg)
                .setPositiveButton(R.string.error_activity_error_details_close, null)
                .setNegativeButton(R.string.error_activity_error_details_send) { _, _ ->
                    try {
                        startActivity(Intent.createChooser(Intent(Intent.ACTION_SEND)
                            .setType("text/plain")
                            .putExtra(Intent.EXTRA_TEXT, errorMsg),
                            this@ErrorActivity.getString(R.string.error_activity_error_details_send)))
                    } catch (e: Exception) {
                        ToastHelper.toastMessage(R.string.unknown_error)
                    }
                }
                .show()
            val textView = dialog.findViewById<View>(android.R.id.message) as TextView
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen.customactivityoncrash_error_activity_error_details_text_size))
        }

    }
}